var emailflag    = false;
var passwordflag = false;
var nikenameflag = false;
var mobileflag   = false;
var graduatetimeflag   = false;
var lastschoolflag   = false;
var placeflag   = false;

function login() {
  art.dialog.open('/login', {
    id : 'login',
    width:460,
    height:400,
    lock : true,
    esc : false,
    fixed : true,
    resize : false,
    drag : false,
    zIndex : 9999
  });
}

function getPassword() {
  art.dialog.open('/login/getPassword', {
    id : 'getPassword',
    lock : true,
    esc : false,
    fixed : true,
    resize : false,
    drag : false,
    zIndex : 9999
  });
}

function actregwin(){
  art.dialog.open('/reg/actregwin', {
    id : 'actregwin',
    width : 460,
    height : 460,
    lock : true,
    esc : false,
    fixed : true,
    resize : false,
    drag : false,
    zIndex : 9999
  });
}

function exit() {
  art.dialog.confirm("确认要退出吗？", function() {
    $.ajax({
      type : 'POST',
      url : '/login/out',
      dataType : 'json',
      success : function(r) {
        if (r.status == 1) {
          window.location.reload();
        }
      }
    });
  });
}

function openreg() {
  window.open('/register');
  art.dialog.close();
}

function counttotal() {
  var e = $('#top_reg_e').attr('value');
  var z = $('#top_reg_z').attr('value');
  var v = e != '' && z != '' ? parseInt(e) + parseInt(z) : '';
    $('#top_reg_t').attr('value', v);
}

function checkEmail()
{
  var Val = $('#email').attr('value');
  var projectid = $('#projectid').attr('value');
  if(IsMail(Val)){
	$.ajax({
	   async:false,
	   type: "GET",
	   url: "?ct=index&ac=check_mail",
	   data: "email="+Val+"&projectid="+projectid,
	   success: function(msg){
		 if(1== msg){
             $('#emailtip').html("<span style='color:#d55437;'>该用户已存在！</span>");
             $('#email').focus();
             // $('#logintip').show();
		 } else {
             $('#emailtip').html("<img src='" + URL + "/wenda/static/images/register/yxk_zc_right.jpg' />该邮箱邮箱可以使用");
			 emailflag = true;
             $('#logintip').hide();
		 }
	   }
	});
  } else {
      $('#emailtip').html(" <img src='" + URL + "/wenda/static/images/register/yxk_zc_wrong.jpg' /><span style='color:#d55437;'> 邮箱不正确！</span>");
      $('#email').focus();
      $('#logintip').hide();
  }
}


function vPassword(){
    var Val1 = $('#password').attr('value');
    var Val2 = $('#repassword').attr('value');

    if(Val1==''){
        $('#passwordtip').html(" <img src='" + URL + "/wenda/static/images/register/yxk_zc_wrong.jpg' /><span style='color:#d55437;'>密码不能为空！</span>");
        $('#password').focus();
    } else {
      $('#passwordtip').html("");
      if(Val1.length<3){
        $('#passwordtip').html(" <img src='" + URL + "/wenda/static/images/register/yxk_zc_wrong.jpg' /><span style='color:#d55437;'>密码不能少于3个字符！</span>");
        $('#password').focus();
      } else {
        if(Val1 != Val2){
            $('#repasswordtip').html(" <img src='" + URL + "/wenda/static/images/register/yxk_zc_wrong.jpg' /><span style='color:#d55437;'>两次密码不一致，请重新填写！</span>");
            $('#repassword').focus();
        } else {
            // $('#passwordtip').html(" <img src='" + URL + "/wenda/static/images/register/yxk_zc_right.jpg' />");

            // $('#repasswordtip').html(" <img src='" + URL + "/wenda/static/images/register/yxk_zc_right.jpg' />");
            $('#repasswordtip').html("");
			passwordflag = true;
        }
      }
    }
}

function vRealName(){
    var Val = $('#realname').attr('value');

    if(Val==''){
        $('#realnametip').html(" <img src='" + URL + "/wenda/static/images/register/yxk_zc_wrong.jpg' /><span style='color:#d55437;'>真实姓名不能为空!</span>");
        $('#realname').focus();
    } else {
      if(Val.length<2){
        $('#realnametip').html(" <img src='" + URL + "/wenda/static/images/register/yxk_zc_wrong.jpg' /><span style='color:#d55437;'>真实姓名不能少于2个字符!</span>");
        $('#realname').focus();
      } else {
         $('#realnametip').html("<img src='" + URL + "/wenda/static/images/register/yxk_zc_right.jpg' />");
         $('#realnametip').html("");
		nikenameflag = true;
      }
    }
}

function vTel(){
  var Val = $('#tel').attr('value');
  var regPartton=/1[3-8]+\d{9}/;

    if(Val==''){
        $('#teltip').html("<span style='color:#d55437;'> 电话号码不能为空！</span>");
		$('#tel').focus();
    } else if (!regPartton.test(Val)){
        $('#teltip').html("<span style='color:#d55437;'> 手机号格式不对！</span>");
    }else {
         // $('#teltip').html("<img src='" + URL + "/wenda/static/images/register/yxk_zc_right.jpg' />");
         $('#teltip').html("");
		mobileflag = true;
    }
}

function vGraduatetime(){
  var Val = $('#graduatetime').attr('value');

    if(Val==''){
        $('#graduatetimeTip').html("<img src='" + URL + "/wenda/static/images/register/yxk_zc_wrong.jpg' /><span style='color:#d55437;'> 请填写是哪年毕业的，比如：2010年！</span>");
		$('#graduatetime').focus();
    } else {
         if(!/^[1|2]\d{3}$/.test(Val)){
            $('#graduatetimeTip').html("<img src='" + URL + "/wenda/static/images/register/yxk_zc_wrong.jpg' /><span style='color:#d55437;'> 年份格式错误！</span>");
            $('#graduatetime').focus();
         }else{
          // $('#graduatetimeTip').html("<img src='" + URL + "/wenda/static/images/register/yxk_zc_right.jpg' />");
          $('#graduatetimeTip').html("");
          graduatetimeflag = true;
         }

    }
}

function vLastschool(){
  var Val = $('#lastschool').attr('value');

    if(Val==''){
        $('#lastschoolTip').html("<span style='color:#d55437;'> 具体毕业院校不能为空！</span>");
		$('#lastschool').focus();
    } else {
         // $('#lastschoolTip').html("<img src='" + URL + "/wenda/static/images/register/yxk_zc_right.jpg' />");
         $('#lastschoolTip').html("");
		lastschoolflag = true;
    }
}

function vPlace(){
  var Val = $('#place').attr('value');

    if(Val==''){
        $('#placeTip').html("<span style='color:#d55437;'> 最近的工作单位不能为空！</span>");
		$('#place').focus();
    } else {
         // $('#placeTip').html("<img src='" + URL + "/wenda/static/images/register/yxk_zc_right.jpg' />");
         $('#placeTip').html("");
		placeflag = true;
    }
}

function adduser(){

	email        = $('#email').attr('value');
	password     = $('#password').attr('value');
       repassword  = $('#repassword').attr('value');
	realname     = $('#realname').attr('value');
	tel           = $('#tel').attr('value');
	degree       = $('#degree').attr('value');
	graduatetime = $('#graduatetime').attr('value');
	lastschool   = $('#lastschool').attr('value');
	place       = $('#place').attr('value');
	projectid    = $('#projectid').attr('value');

    // 获取选择的学校
    yxxys = $('input[name=target_choice_school_ids]').val();

    // 开始验证考生填写的信息是否真确
    checkEmail();
    if(!emailflag) {
        return false;
    }

/*    vPassword();
    if(!passwordflag) {
        return false;
    }*/

    vRealName();
    if(!nikenameflag) {
        return false;
    }

    vTel();
    if(!mobileflag) {
        return false;
    }

    vGraduatetime();
    if(!graduatetimeflag) {
        return false;
    }

    vPlace();
    if(!placeflag) {
        return false;
    }

        // if(!emailflag || !passwordflag || !nikenameflag || !mobileflag || !graduatetimeflag || !placeflag) {
	if(!emailflag  || !nikenameflag || !mobileflag || !graduatetimeflag || !placeflag) {
		alert('填写有误，请填写您的正确信息！');
		return false;
	}

    if(yxxys == "") {
        alert("你至少要选择一个学校！");
        return false;
    }

	// 提交到数据库
	$.ajax({
	   type: "POST",
	   url: "?ct=index&ac=register",
     beforeSend:function(){
       art.dialog({
        id:'regtips',
        content:'请稍候...',
        lock : true,
        esc : false,
        fixed : true,
        resize : false,
        drag : false,
        cancel : false
       });
     },
          //data: "email="+email+"&password="+password+"&realname="+realname+"&tel="+tel+"&itemid="+projectid+"&yxxys="+yxxys+"&degree="+degree+"&graduatetime="+graduatetime+"&lastschool="+lastschool+"&place="+place+"&adduser=adduser",
	   data: "email="+email+"&realname="+realname+"&tel="+tel+"&itemid="+projectid+"&yxxys="+yxxys+"&degree="+degree+"&graduatetime="+graduatetime+"&lastschool="+lastschool+"&place="+place+"&adduser=adduser",
	   success: function(msg){
     art.dialog({id:'regtips'}).close();
     if(1== msg){
      art.dialog.tips('对不起，报名失败，请重新报名');
     } else {
        alert("报名注册成功！");
        location.href=URL;
     }
     }
	});

    return true;
}

function IsMail(mail)    //验证邮箱格式
{
	var patrn = /^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/;
	if (!patrn.test(mail)){
		return false;
	}else{
		return true;
	}
}